﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OctoOS.UI;
using OctoOS;

public partial class myAds : OOSPage
{
    class PathInfo
    {
        [PathInfoMember(0)]
        internal byte type;

        [PathInfoMember(1, true)]
        internal byte status;

        [PathInfoMember(2, true)]
        internal int pageIndex;

        internal void PostInit()
        {
            if (pageIndex > 0)
            {
                pageIndex--;
            }
        }
    }

    PathInfo _pathInfo;
    int _adsOnPage = 2;
    int _startIndex;
    ClassifiedAd[] _ads;
    private int _totalActive, _totalPending, _totalHidden, _totalExpired = 0;


    protected void Page_Init(object sender, EventArgs e)
    {
        OOSRequest.Current.EnsureSignIn();
        _pathInfo = EnablePathInfo<PathInfo>();
        Helper.Categories.UpdateCategoryCache();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        SetBreadcrumbs();
        switch (_pathInfo.type)
        {
            case (byte)Helper.AdType.Offer:
                PageTitle = "My Offer Ads";
                break;
            case (byte)Helper.AdType.Need:
                PageTitle = "My Need Ads";
                break;
        }
        
        switch (_pathInfo.status)
        {
            case (int)Helper.AdStatus.Active:
                AdTabs.SelectTab("Active");
                break;
            case (int)Helper.AdStatus.Pending:
                AdTabs.SelectTab("Pending");
                break;
            case (int)Helper.AdStatus.Expired:
                AdTabs.SelectTab("Expired");
                break;
            case (int)Helper.AdStatus.Declined:
                AdTabs.SelectTab("Declined");
                break;
        }
        AddAction("Offer Ads", Urls.Classifieds.GetMyAds(0, 0));
        AddAction("Need Ads", Urls.Classifieds.GetMyAds(1, 0));
        AdTabs.Tabs["Active"].HRef = Urls.Classifieds.GetMyAds(_pathInfo.type, (int)Helper.AdStatus.Active);
        AdTabs.Tabs["Pending"].HRef = Urls.Classifieds.GetMyAds(_pathInfo.type, (int)Helper.AdStatus.Pending);
        AdTabs.Tabs["Declined"].HRef = Urls.Classifieds.GetMyAds(_pathInfo.type, (int)Helper.AdStatus.Declined);
        AdTabs.Tabs["Expired"].HRef = Urls.Classifieds.GetMyAds(_pathInfo.type, (int)Helper.AdStatus.Expired);

        _startIndex = _pathInfo.pageIndex * _adsOnPage;

        if (_pathInfo.status != (byte)Helper.AdStatus.Expired)
        {
            _ads = DataSource.ClassifiedAds_GetMyNonExpired(
                OOSRequest.Current.ResourceID,
                OOSRequest.Current.UserID,
                _pathInfo.type,
                _pathInfo.status,
                _startIndex,
                _adsOnPage,
                out _totalActive,
                out _totalPending,
                out _totalHidden,
                out _totalExpired);
        }
        else
        {
            _ads = DataSource.ClassifiedAds_GetMyExpired(
                OOSRequest.Current.ResourceID,
                OOSRequest.Current.UserID,
                _pathInfo.type,
                _startIndex,
                _adsOnPage,
                out _totalActive,
                out _totalPending,
                out _totalHidden,
                out _totalExpired); 
        }

        // set tab names
        AdTabs.Tabs["Active"].Text = AdTabs.Tabs["Active"].Text + " (" + _totalActive + ")";
        AdTabs.Tabs["Pending"].Text = AdTabs.Tabs["Pending"].Text + " (" + _totalPending + ")";
        AdTabs.Tabs["Declined"].Text = AdTabs.Tabs["Declined"].Text + " (" + _totalHidden + ")";
        AdTabs.Tabs["Expired"].Text = AdTabs.Tabs["Expired"].Text + " (" + _totalExpired + ")";

        if (_ads.Length > 0)
        {
            int pageMaxCnt = 0;
            switch (_pathInfo.status)
            {
                case (byte)Helper.AdStatus.Active:
                    pageMaxCnt = Convert.ToInt32(Math.Ceiling((double)_totalActive / _adsOnPage));
                    break;
                case (byte)Helper.AdStatus.Pending:
                    pageMaxCnt = Convert.ToInt32(Math.Ceiling((double)_totalPending / _adsOnPage));
                    break;
                case (byte)Helper.AdStatus.Declined:
                    pageMaxCnt = Convert.ToInt32(Math.Ceiling((double)_totalHidden / _adsOnPage));
                    break;
                case (byte)Helper.AdStatus.Expired:
                    pageMaxCnt = Convert.ToInt32(Math.Ceiling((double)_totalExpired / _adsOnPage));
                    break;
            }
            adsNumericPager.PageCount = pageMaxCnt;
            adsNumericPager.SelectedIndex = _pathInfo.pageIndex;
            adsNumericPager.PageUrlPattern = Urls.Classifieds.GetMyAds(_pathInfo.type, _pathInfo.status) + "/{0}";
            adsNumericPager.FirstPageUrl = Urls.Classifieds.GetMyAds(_pathInfo.type, _pathInfo.status);

            ShowFoundAds(_ads);
        }
        else
        {
            Msg.Visible = true;
        }
    }

    private void ShowFoundAds(ClassifiedAd[] ads)
    {
        foreach (var ad in ads)
        {
            OctoOS.UI.ListViewItem row = new OctoOS.UI.ListViewItem();
            row.Controls.Add(GetObjectPreview(ad));
            ListView.Controls.Add(row);
        }
    }

    private ObjectPreview GetObjectPreview(ClassifiedAd ad)
    {
        Image48ObjectPreview pr = new Image48ObjectPreview();
        pr.ImageUrl = "/Images/email_icon.jpg";
        pr.HeaderText = ad.Title;
        pr.HeaderUrl = Urls.Classifieds.GetInfoAd(ad.ID);
        pr.Footer = new OctoOS.UI.Literal(GetCategoriesFooter(ad.CategoryID));
        pr.Body = new OctoOS.UI.Literal("Post Time: " + ad.PostTime.ToShortDateString() + ", " + ad.PostTime.ToShortTimeString());
        return pr;
    }

    private string GetCategoriesFooter(long categoryID)
    {
        string result = "Uncategorized";
        Category grandParentCategory = Helper.Categories.GetGrandParentCategory(categoryID);
        Category parentCategory = Helper.Categories.GetParentCategory(categoryID);
        Category category = Helper.Categories.GetCategory(categoryID);
        if (category != null)
        {
            result = category.Name;
            if (category.ParentCategoryID != 0)
            {
                result = parentCategory.Name + " -> " + result;
                if (parentCategory.ParentCategoryID != 0)
                {
                    result = grandParentCategory.Name + " -> " + result;
                }
            }
        }
        return "Category: " + result;
    }

    private void SetBreadcrumbs()
    {
        AddBreadcrumb(string.Empty, "/");
    }

}